package com.sims.daoImp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.SeatChart;
import com.sims.dao.ISeatChartDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * SeatChart entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.sims.bean.SeatChart
 * @author MyEclipse Persistence Tools
 */

public class SeatChartDAO extends HibernateDaoSupport implements ISeatChartDAO {
	private static final Logger log = LoggerFactory
			.getLogger(SeatChartDAO.class);
	// property constants
	public static final String CID = "cid";
	public static final String SID = "sid";
	public static final String ROW = "row";
	public static final String COL = "col";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#save(com.sims.bean.SeatChart)
	 */
	@Override
	public void save(SeatChart transientInstance) {
		log.debug("saving SeatChart instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#delete(com.sims.bean.SeatChart)
	 */
	@Override
	public void delete(SeatChart persistentInstance) {
		log.debug("deleting SeatChart instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#findById(java.lang.Integer)
	 */
	@Override
	public SeatChart findById(java.lang.Integer id) {
		log.debug("getting SeatChart instance with id: " + id);
		try {
			SeatChart instance = (SeatChart) getHibernateTemplate().get(
					"com.sims.bean.SeatChart", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#findByExample(com.sims.bean.SeatChart)
	 */
	@Override
	public List findByExample(SeatChart instance) {
		log.debug("finding SeatChart instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SeatChart instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from SeatChart as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#findByCid(java.lang.Object)
	 */
	@Override
	public List findByCid(Object cid) {
		return findByProperty(CID, cid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#findBySid(java.lang.Object)
	 */
	@Override
	public List findBySid(Object sid) {
		return findByProperty(SID, sid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#findByRow(java.lang.Object)
	 */
	@Override
	public List findByRow(Object row) {
		return findByProperty(ROW, row);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#findByCol(java.lang.Object)
	 */
	@Override
	public List findByCol(Object col) {
		return findByProperty(COL, col);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all SeatChart instances");
		try {
			String queryString = "from SeatChart";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#merge(com.sims.bean.SeatChart)
	 */
	@Override
	public SeatChart merge(SeatChart detachedInstance) {
		log.debug("merging SeatChart instance");
		try {
			SeatChart result = getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#attachDirty(com.sims.bean.SeatChart)
	 */
	@Override
	public void attachDirty(SeatChart instance) {
		log.debug("attaching dirty SeatChart instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ISeatChartDAO#attachClean(com.sims.bean.SeatChart)
	 */
	@Override
	public void attachClean(SeatChart instance) {
		log.debug("attaching clean SeatChart instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ISeatChartDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ISeatChartDAO) ctx.getBean("SeatChartDAO");
	}
	
	public List findStudentSeatChartByCid(Object cid) {
		String queryString = "select new com.sims.beanEnhanced.StudentSeatChart(s,sc) " 
				+ "from Student s, SeatChart sc where "
				+ "s.cid = :scid and "
				+ "s.sid = sc.sid";
		org.hibernate.Query query = getSession().createQuery(queryString);
		query.setInteger("scid", (Integer) cid);
		return query.list();
	}
}